
#include <EEPROM.h>

//#define MAIN_Fosc		22118400L

void	DisableEEPROM(void)
{
	ISP_CONTR = 0;			
	ISP_CMD   = 0;			
	ISP_TRIG  = 0;			
	ISP_ADDRH = 0xff;		
	ISP_ADDRL = 0xff;		
}



void EEPROM_read_n(u16 EE_address,u8 *DataAddress,u16 number)
{
	EA = 0;		//????
	ISP_CONTR = (ISP_EN + ISP_WAIT_FREQUENCY);	//??????,??ISP/IAP??,?????
	ISP_READ();									//??????,???????,???????
	do
	{
		ISP_ADDRH = EE_address / 256;		//??????(??????????????)
		ISP_ADDRL = EE_address % 256;		//??????
		ISP_TRIG();							//??5AH,??A5H?ISP/IAP?????,???????
											//??A5H?,ISP/IAP?????????
											//CPU??IAP???,?????????
		_nop_();
		*DataAddress = ISP_DATA;			//???????
		EE_address++;
		DataAddress++;
	}while(--number);

	DisableEEPROM();
	EA = 1;		//??????
}



void EEPROM_SectorErase(u16 EE_address)
{
	EA = 0;		//????
											//??????,??????,512??/???
											//??????????????????
	ISP_ADDRH = EE_address / 256;			//????????(??????????????)
	ISP_ADDRL = EE_address % 256;			//????????
	ISP_CONTR = (ISP_EN + ISP_WAIT_FREQUENCY);	//??????,??ISP/IAP??,?????
	ISP_ERASE();							//???????,???????,???????
	ISP_TRIG();
	_nop_();
	DisableEEPROM();
	EA = 1;		//??????
}


void EEPROM_write_n(u16 EE_address,u8 *DataAddress,u16 number)
{
	EA = 0;		//????

	ISP_CONTR = (ISP_EN + ISP_WAIT_FREQUENCY);	//??????,??ISP/IAP??,?????
	ISP_WRITE();							//??????,???????,???????
	do
	{
		ISP_ADDRH = EE_address / 256;		//??????(??????????????)
		ISP_ADDRL = EE_address % 256;		//??????
		ISP_DATA  = *DataAddress;			//????ISP_DATA,????????????
		ISP_TRIG();
		_nop_();
		EE_address++;
		DataAddress++;
	}while(--number);

	DisableEEPROM();
	EA = 1;		//??????
}